<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category CategoryController.php
 * @Depiction:
 * Author: hwy
 * Date: 2020/6/17  16:19
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Admin;

use App\Exceptions\EmptyException;
use App\Http\Controllers\Controller;
use App\Http\Requests\Category\Store;
use App\Http\Requests\CategoryStoreRequest;
use App\Http\Requests\CategoryUpdateRequest;
use App\Models\Category;
use App\Services\CategoryService;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class CategoryController extends BaseController
{
    /**
     * @var string
     */
    protected $_model = 'category';

    /**
     * @var 栏目服务类
     */
    protected $categoryService;

    /**
     * CategoryController constructor.
     */
    public function __construct()
    {
        $this->categoryService = CategoryService::getInstance();
    }

    /**
     * @param Request $request
     * @return Factory|View
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  11:32
     */
    public function index(Request $request)
    {
        $compact = compact('request');

        return view('admin.' . $this->_model . '.index', $compact);

    }

    /**
     * @param Request $request
     * @return JsonResponse
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  11:32
     */
    public function getList(Request $request)
    {
        $responseData = $this->categoryService->getList($request);

        return response()->json($responseData);
    }

    /**
     * @param Request $request
     * @return Factory|View
     * @Depiction
     * @Author hwy
     * @Date  2020/6/17  16:19
     */
    public function create(Request $request)
    {
        $category = $this->categoryService->getCateLevel();

        return view('admin.' . $this->_model . '.create', compact('request', 'category'));
    }

    /**
     * @param CategoryStoreRequest $request
     * @return JsonResponse
     * @Depiction
     * @Author hwy
     * @Date  2020/6/17  16:19
     */
    public function store(CategoryStoreRequest $request)
    {
        $responseData = $this->categoryService->addData($request);

        return response()->json($responseData);

    }

    /**
     * @param Request $request
     * @param $id
     * @return Factory|View
     * @Depiction
     * @Author hwy
     * @Date  2020/6/17  16:19
     */
    public function edit(Request $request, $id)
    {
        $data     = $this->categoryService->getOne($id);
        $category = $this->categoryService->getCateLevel();

        return view('admin.' . $this->_model . '.edit', compact('request', 'data', 'category'));

    }

    /**
     * @param CategoryUpdateRequest $request
     * @param $id
     * @return JsonResponse
     * @throws EmptyException
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  16:58
     */
    public function update(CategoryUpdateRequest $request, $id)
    {
        $responseData = $this->categoryService->editData($request, $id);

        return response()->json($responseData);
    }

    /**
     * @param $id
     * @return JsonResponse
     * @throws EmptyException
     * @Depiction
     * @Author hwy
     * @Date  2021/2/22  16:58
     */
    public function destroy($id)
    {
        $responseData = $this->categoryService->delData($id);

        return response()->json($responseData);
    }

}
